package com.tarzan.maxkb4j.module.application.chat.actuator;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.tarzan.maxkb4j.core.workflow.INode;
import com.tarzan.maxkb4j.core.workflow.NodeFactory;
import com.tarzan.maxkb4j.core.workflow.WorkflowManage;
import com.tarzan.maxkb4j.core.workflow.domain.FlowParams;
import com.tarzan.maxkb4j.core.workflow.enums.NodeType;
import com.tarzan.maxkb4j.core.workflow.logic.LfNode;
import com.tarzan.maxkb4j.core.workflow.logic.LogicFlow;
import com.tarzan.maxkb4j.module.application.cache.ChatCache;
import com.tarzan.maxkb4j.module.application.chat.base.ChatBaseActuator;
import com.tarzan.maxkb4j.module.application.domian.dto.ChatInfo;
import com.tarzan.maxkb4j.module.application.domian.dto.ChatMessageDTO;
import com.tarzan.maxkb4j.module.application.domian.entity.ApplicationEntity;
import com.tarzan.maxkb4j.module.application.domian.entity.ApplicationWorkFlowVersionEntity;
import com.tarzan.maxkb4j.module.application.domian.vo.ApplicationChatRecordVO;
import com.tarzan.maxkb4j.module.application.handler.PostResponseHandler;
import com.tarzan.maxkb4j.module.application.service.ApplicationChatRecordService;
import com.tarzan.maxkb4j.module.application.service.ApplicationWorkFlowVersionService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;

@AllArgsConstructor
@Component
public class ChatFlowActuator extends ChatBaseActuator {

    private final ApplicationWorkFlowVersionService workFlowVersionService;
    private final ApplicationChatRecordService chatRecordService;
    private final PostResponseHandler postResponseHandler;

    @Override
    public String chatOpenTest(ApplicationEntity application) {
        ChatInfo chatInfo = new ChatInfo();
        chatInfo.setChatId(IdWorker.get32UUID());
        application.setId(null);
        ApplicationWorkFlowVersionEntity workflowVersion = new ApplicationWorkFlowVersionEntity();
        workflowVersion.setWorkFlow(application.getWorkFlow());
/*        application.setDialogueNumber(3);
        application.setType(application.getType());
        application.setUserId(StpUtil.getLoginIdAsString());*/
        chatInfo.setApplication(application);
        LogicFlow logicFlow=LogicFlow.newInstance(application.getWorkFlow());
        List<LfNode> lfNodes=logicFlow.getNodes();
        List<INode> nodes=lfNodes.stream().filter(lfNode -> !NodeType.BASE.getKey().equals(lfNode.getType())).map(NodeFactory::getNode).toList();
        chatInfo.setNodes(nodes);
        chatInfo.setEdges(logicFlow.getEdges());
        ChatCache.put(chatInfo.getChatId(), chatInfo);
        return chatInfo.getChatId();
    }

    @Override
    public String chatOpen(ApplicationEntity application, String chatId) {
        ChatInfo chatInfo = new ChatInfo();
        chatInfo.setChatId(chatId);
        ApplicationWorkFlowVersionEntity workFlowVersion = workFlowVersionService.lambdaQuery()
                .eq(ApplicationWorkFlowVersionEntity::getApplicationId, application.getId())
                .orderByDesc(ApplicationWorkFlowVersionEntity::getCreateTime)
                .last("limit 1").one();
        chatInfo.setApplication(application);
        LogicFlow logicFlow=LogicFlow.newInstance(workFlowVersion.getWorkFlow());
        List<LfNode> lfNodes=logicFlow.getNodes();
        List<INode> nodes=lfNodes.stream().filter(lfNode -> !NodeType.BASE.getKey().equals(lfNode.getType())).map(NodeFactory::getNode).toList();
        chatInfo.setNodes(nodes);
        chatInfo.setEdges(logicFlow.getEdges());
        ChatCache.put(chatInfo.getChatId(), chatInfo);
        return chatId;
    }

    @Override
    public String chatMessage(ChatInfo chatInfo,ChatMessageDTO dto) {
        chatCheck(chatInfo,dto);
        ApplicationChatRecordVO chatRecord = null;
        String chatRecordId = dto.getChatRecordId();
        if(StringUtils.isNotBlank(chatRecordId)){
            chatRecord = chatRecordService.getChatRecordInfo(chatInfo, chatRecordId);
        }
        FlowParams flowParams = new FlowParams();
        flowParams.setChatId(chatInfo.getChatId());
        flowParams.setChatRecordId(dto.getChatRecordId() == null ? IdWorker.get32UUID() : dto.getChatRecordId());
        flowParams.setQuestion(dto.getMessage());
        flowParams.setReChat(dto.getReChat());
        flowParams.setClientId(dto.getClientId());
        flowParams.setClientType(dto.getClientType());
        flowParams.setStream(dto.getStream() == null || dto.getStream());
        flowParams.setHistoryChatRecord(chatInfo.getChatRecordList());//添加历史记录
        WorkflowManage workflowManage = new WorkflowManage(
                chatInfo.getNodes(),
                chatInfo.getEdges(),
                flowParams,
                dto.getSink(),
                postResponseHandler,
                dto.getGlobalData(),
                dto.getImageList(),
                dto.getDocumentList(),
                dto.getAudioList(),
                dto.getRuntimeNodeId(),
                dto.getNodeData(),
                chatRecord);
        return workflowManage.run();
    }

}
